package cn.com.lms.business.emprev.web;

import cn.com.lms.business.core.util.SystemUserUtil;
import cn.com.lms.business.emprev.persistence.entity.EmpRecordMaintenance;
import cn.com.lms.business.emprev.service.EmpRecordMaintenanceService;
import cn.com.lms.business.organization.persistence.entity.FbSysDepartment;
import cn.com.lms.business.organization.service.FbSysDepartmentService;
import cn.com.lms.business.user.persistence.entity.SysUserVo;

import com.bnzj.core.exception.BusinessException;
import com.bnzj.core.persistence.query.CompareExpression;
import com.bnzj.core.persistence.query.CompareType;
import com.bnzj.core.persistence.query.IExpression;

import com.bnzj.core.persistence.query.QueryHelpBean;
import com.bnzj.core.rest.ResponseResult;
import com.bnzj.core.rest.ResultCode;
import com.bnzj.core.webmvc.util.WebMvcUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static cn.com.lms.business.constant.LmsConstant.ASCEND;
import static cn.com.lms.business.constant.LmsConstant.DESCEND;

/**
 * @Author: 16266
 * @DateTime: 2020/3/18 14:45
 * @Description: 备案维护控制器
 */
@Tag(name = "员工审报", description = "备案维护")
@RestController
@RequestMapping(value = "/rec/mtc")
public class EmpRecordMaintenanceController {

    @Autowired
    private EmpRecordMaintenanceService empRecordMaintenanceService;

    @Autowired
    FbSysDepartmentService fbSysDepartmentService;

    @Operation(summary = "分页查询", description = "", parameters = {
            @Parameter(name = "offset", description = "起始页", schema = @Schema(name = "offset", description = "", implementation = String.class)),
            @Parameter(name = "limit", description = "页大小", schema = @Schema(name = "limit", description = "", implementation = String.class)),
            @Parameter(name = "personnel", description = "在职状态", schema = @Schema(name = "personnel", description = "", implementation = String.class)),
            @Parameter(name = "isRecord", description = "是否备案", schema = @Schema(name = "isRecord", description = "", implementation = Boolean.class)),
            @Parameter(name = "keyWord", description = "目录名称进行模糊搜索", schema = @Schema(name = "keyWord", description = "", implementation = String.class)),
            @Parameter(name = "timeSort", description = "时间排序", schema = @Schema(name = "timeSort", description = "", implementation = String.class))
    })
    @GetMapping(value = "/page")
    public ResponseResult<Page<EmpRecordMaintenance>> getEmpRecordMaintenance(HttpServletRequest request) {
        SysUserVo currentUser = SystemUserUtil.getCurrentUser();
        String account = currentUser.getAccount();
        EmpRecordMaintenance empRecordMaintenance = empRecordMaintenanceService.findByAccount(account);
        //设置部门为空
        Long userDeptId = null;
        if (ObjectUtils.isNotEmpty(empRecordMaintenance)) {
            //根据登陆人ID获取部门名称查询数据 只限用户权限
            userDeptId = empRecordMaintenance.getDeptId();
        }
        if (null == userDeptId || 0L == userDeptId) {
            return ResponseResult.bizException(new BusinessException("登录人缺少部门权限",
                    String.valueOf(ResultCode.FAILURE)));
        }
        // 获取登录人所属部门下的所有部门id（包括自己部门）
        List<FbSysDepartment> deptList = fbSysDepartmentService.getEnabledDeptTreeList();
        List<Long> includeDeptIds = fbSysDepartmentService.specificDeptChildIds(userDeptId, deptList);
        includeDeptIds.add(userDeptId);

        Pageable pageable = null;
        //判断参数排序
        if (ASCEND.equals(request.getParameter("timeSort"))) {
            pageable = WebMvcUtils.initPage(request, Sort.by(Sort.Direction.ASC, "entry_date"));
        } else if (DESCEND.equals(request.getParameter("timeSort"))) {
            pageable = WebMvcUtils.initPage(request, Sort.by(Sort.Direction.DESC, "entry_date"));
        } else {
            pageable = WebMvcUtils.initPage(request, Sort.by(Sort.Direction.DESC, "entry_date"));
        }
        QueryHelpBean dicQueryBean = empRecordMaintenanceService.getQueryHelpBean();
        dicQueryBean.setPageable(pageable);
        //查询已启用数据
        IExpression orCondition = CompareExpression.build("d.enabled", CompareType.equal, true);
        //数据权限
        IExpression orCondition1 = CompareExpression.build("d.dept_id", CompareType.in, includeDeptIds);
        //是否备案
        IExpression orCondition2 = CompareExpression.build("d.is_record", CompareType.equal, request.getParameter("isRecord"));
        //在职状态
        IExpression orCondition3 = CompareExpression.build("d.personnel", CompareType.equal, request.getParameter("personnel"));
        IExpression orCondition4 = CompareExpression.build("d.account", CompareType.like, request.getParameter("keyWord"));
        IExpression orCondition5 = CompareExpression.build("d.cname", CompareType.like, request.getParameter("keyWord"));
        IExpression orCondition6 = CompareExpression.build("d.full_dept_name", CompareType.like, request.getParameter("keyWord"));
        dicQueryBean.addWhere(orCondition.and(orCondition1.and(orCondition2.and(orCondition3.and(orCondition4.or(orCondition5).or(orCondition6))))));
        Page<EmpRecordMaintenance> page = empRecordMaintenanceService.findAll(dicQueryBean);
        return ResponseResult.success(page);
    }

}